package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushougeproduct.pojo.param.PictureAddNewParam;
import cn.tedu.yushougeproduct.pojo.param.PictureUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.vo.*;
import cn.tedu.yushougeproduct.service.IPictureService;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/picture/")
@Slf4j
@Api(tags = "06：图片管理模块")
@Validated
public class PictureController {

    @Autowired
    IPictureService iPictureService;


    @PostMapping("add-new")
    @ApiOperation("添加图片")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid PictureAddNewParam pictureAddNewParam) {
        log.debug("开始处理【添加图片】的请求，参数：{}", pictureAddNewParam);
        iPictureService.addNew(pictureAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("根据ID删除图片")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除图片失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除图片】的请求，参数：{}", id);
        iPictureService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping ("setCover")
    @ApiOperation("根据ID将图片设置为封面")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setCover(@Range(min = 1, message = "请提交有效的图片ID值！")
                                     @RequestParam Long id)  {
        log.debug("开始处理【根据ID将图片设置为封面】的请求，参数：{}", id);
        iPictureService.setCover(id);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询图片详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID", required = true, dataType = "long")
    })
    public JsonResult<PictureStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询图片详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询图片详情】的请求，参数：{}", id);
        PictureStandardVO result = iPictureService.getStandardById(id);
        return JsonResult.ok(result);
    }


    @GetMapping("listByAlbumId")
    @ApiOperation("查询相册的图片列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "albumId", value = "相册Id", paramType = "query")
    })
    public JsonResult<List<PictureListItemVO>> list(@Range(min = 1, message = "查询图片失败，请提供正确的相册ID！") Long albumId) {
        log.debug("开始处理【查询相册的图片列表】的请求，父类id：{}", albumId);
        List<PictureListItemVO> list = iPictureService.listByAlbumId(albumId);
        return JsonResult.ok(list);
    }

}
